package com.gopay.bis.ccb.ebank.operation;

import java.util.HashMap;
import java.util.Map;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.ccb.ebank.query.req.TX;
import com.gopay.common.domain.bank.ccb.ebank.query.req.TX_INFO;
import com.gopay.common.domain.bank.ccb.ebank.pay.CcbB2cMessage;
import com.gopay.common.exception.GopayException;

/**
 * 构建建设银行B2C交易所需信息
 * 
 * @CcbB2cBuildMessage.java
 * @author WangHongFei
 */
public class CcbB2cBuildMessage {
	
	private final static String XML_HEADER = "<?xml version=\"1.0\" encoding=\"GB2312\" standalone=\"yes\" ?>\n";
	/* 建行商户号 */
	private String merchantid;
	/* 建行操作员 */
	private String operatorid;
	/* 建行操作员登录密码 */
	private String password;

	/**
	 * 构建B2Cxml格式
	 * 
	 * @param CcbB2cMessage
	 * @return String
	 */
	public String buildB2cXmlStr(CcbB2cMessage message) throws GopayException{
		try {
			StringBuffer sb = new StringBuffer(256);
			// 排列按TX的属性排列顺序
			sb.append(XML_HEADER);
			@SuppressWarnings("rawtypes")
			Map<String, Class> cm = new HashMap<String, Class>();
			cm.put("TX", TX.class);
			TX tx = null;
			String xmlStr = null;
			tx = getTxInfo(message);
			xmlStr = XMLBeanUtils.bean2xml(cm, tx);
			sb.append(xmlStr);
			return sb.toString();
		} catch (Exception e) {
			BankLogUtils.printB2CQueryExceptionLog(message, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6035);
		}
	}

	/**
	 * 根据CCBMessage 组装所需类
	 * 
	 * @param msg
	 * @return
	 */
	private TX getTxInfo(CcbB2cMessage msg) {
		TX_INFO tX_INFO = new TX_INFO();
		tX_INFO.setSTART(msg.getStartDate());
		tX_INFO.setSTARTHOUR("00");
		tX_INFO.setSTARTMIN("00");
		tX_INFO.setEND(msg.getEndDate());
		tX_INFO.setENDHOUR("23");
		tX_INFO.setENDMIN("59");
		tX_INFO.setKIND(msg.getKind());
		tX_INFO.setORDER(msg.getOrder());
		tX_INFO.setACCOUNT(msg.getAccNo());
		tX_INFO.setDEXCEL("1");
		tX_INFO.setMONEY("");
		tX_INFO.setNORDERBY("1");
		tX_INFO.setPAGE(msg.getPage());
		tX_INFO.setPOS_CODE(msg.getPoscode());
		tX_INFO.setSTATUS(msg.getStatus());
		TX tx = new TX();
		tx.setREQUEST_SN(msg.getRequestSn());
		tx.setCUST_ID(this.merchantid);
		tx.setUSER_ID(this.operatorid);
		tx.setPASSWORD(this.password);
		tx.setTX_CODE(msg.getTxCode());
		tx.setLANGUAGE(msg.getLanguage());
		tx.setTX_INFO(tX_INFO);
		return tx;
	}

	public void setMerchantid(String merchantid) {
		this.merchantid = merchantid;
	}

	public void setOperatorid(String operatorid) {
		this.operatorid = operatorid;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
